package com.wdz.mvr.controllers;

import com.wdz.mvr.Services.ActorService;
import com.wdz.mvr.Services.MovieService;
import com.wdz.mvr.dao.ActorRepository;
import com.wdz.mvr.entities.Actor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("actors")
public class ActorController {

    @Autowired
    ActorService actorService;

    @Autowired
    MovieService movieService;

    @GetMapping
    public String displayActors(Model model) {

        List<Actor> actors = actorService.getAll();
        model.addAttribute("Actors", actors);
        return "actors/list-actors";

    }

    @GetMapping("/new")
    public String displayActorForm(Model model) {

        Actor aActor = new Actor();
//        List<Classification> classifications=classificationService.getAll();
        model.addAttribute("actor", aActor);
//        model.addAttribute("allClassifications",classifications);
        return "actors/new-actor";

    }


    @PostMapping("/save")
    public String createActor(Model model, @RequestParam List<Long> classifications, Actor actor) {

        //处理保存数据库的行为
        actorService.save(actor);

        //使用重定位防止重复提交
        return "redirect:/actors";
    }
}
